<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" >
<head>
	<title>Loading Topic</title>
	<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
	<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />
	<link rel="icon" type="image/vnd.microsoft.icon" href="images/eckoit.ico">
    <link rel="stylesheet" type="text/css" href="styles/common.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="styles/color.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="styles/wikid2.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="styles/tray.css"  />
	<link type="text/css" href="styles/jquery-ui-1.8.1.custom.css" rel="stylesheet" />	
	<link rel="stylesheet" type="text/css" media="screen, projection" href="styles/horizontal.css" />
    <style media="screen" type="text/css">

    /* --> */
    </style>
    <!--[if lt IE 7]>
    <style media="screen" type="text/css">
    .col1 {
	    width:100%;
	}
    </style>
    <![endif]-->
        <script type="text/javascript" src="scripts/jquery-1.4.2.min.js"></script>
	<script type="text/javascript" src="scripts/jquery-ui-1.8.1.custom.min.js"></script>
	<script type="text/javascript" src="scripts/wikid-0.2.js"></script>
	<script type="text/javascript" src="scripts/jquery.parsequery.js"></script>
	<script type="text/javascript" src="scripts/showdown.js"></script>
	<script type="text/javascript" src="scripts/jsonpath-0.8.0.js"></script>
	 <script type="text/javascript" src="scripts/jquery.corner.js"></script>
	 <script type="text/javascript" src="scripts/couch.js"></script> 
	 <script type="text/javascript" src="scripts/futon.format.js"></script> 
	 <script type="text/javascript" src="scripts/jquery.couch.js"></script> 
	 <script type="text/javascript" src="scripts/jquery.suggest.js"></script> 
	 <script type="text/javascript" src="scripts/jquery.timeago.js"></script>
	 <script type="text/javascript" src="scripts/menu.js"></script>
    <script type="text/javascript" src="scripts/jquery.md5.js"></script>
	 <script type="text/javascript">
	    $(function() {


			$("#dialog-skype").dialog({
				autoOpen: false,
				resizable: false,
				modal: true,
				buttons : {
					'Yes, Start Tagging' : function() {},
					'Cancel' : function() {
						$(this).dialog('close');
					}
				
				}
			});


			var db_name = wikid.parseDBname(window.location);
			var wikid_id = wikid.parseTopicName(window.location);
			var db = new CouchDB(db_name, {"X-Couch-Full-Commit":"true"});
			var doc = db.open(wikid_id);
            if (!doc) {
                $('#taglist').hide();
                var readableId = wikid.titleFromTopicId(wikid_id);
                $('.readableId').text(readableId);
                document.title = wikid.documentTitle(readableId);
                var add = $('<p class="notFound">This document does not exist. <a href="edit.html?' + wikid_id + '">Create it.</a></p>');
                $('#maincontent').append(add);                
                return;
            }
			var result = wikid.toWikid(doc, db, 'wikid');
			$('.readableId').text(result.topic);
            $('#titleHolder').attr('title', wikid_id);
			document.title = result.title;
			if (result.wiki) {
				$('#Wiki').html(result.wiki);
			}
			var lastmodified_val = result.doc['-lastmodified'];
			if (lastmodified_val) {
				var lastmodified = $.timeago(lastmodified_val);
				$('#lastmodified').text('(updated '+ lastmodified + ')');
			}

			$("#editButton").attr('href', 'edit.html?' + wikid_id);

			if (doc['-instance'] && doc['-instance'].schema) {

				$('#addButtonMenu').append($('<li><a href="'+ wikid.getTagEditHref(wikid_id,true) + '">New ' + result.topic +  '</a></li>'));
			} else {
			    var n = $('<li><a href="' + wikid.getTagEditHref(wikid_id,false) +'">Topic Tagged \'' + result.topic +  '\'</a></li>')
			    $('#addButtonMenu').append(n);
            }

			var tools = $('#toolsButtonMenu');
			tools.append('<li><a href="properties.html?' + wikid_id +'">Edit Properties</a></li>');

            var server = window.location.protocol + '//' + window.location.host + '/' + db_name;
			tools.append('<li><a href="/' + db_name +'/_design/tags/_list/freemind/by_id?topic='+wikid_id+'&server='+server+'">Recording Template</a></li>');
			if (result.tagged && result.tagged.length > 0) {
				tools.append('<li><a href="/' + db_name +'/_design/tags/_list/freemind/by_id?topic='+wikid_id+'&tags='+wikid_id+'&server='+server+'">Recording template, related only</a></li>');
			}
			tools.append('<li><a id="deleteTopic" href="#">Delete</a></li>');
			//tools.append('<li class="seperator"></li>');
			tools.append('<li><a href="/_utils/document.html?' + db_name + '/' +  wikid_id + '">View in Futon</a></li>');
			tools.append('<li><a href="/' + db_name + '/' +  wikid_id + '">View Raw JSON</a></li>');

			$('#deleteTopic').click(function() {
				// need a conf dialog
				db.deleteDoc(doc);
				// where do we go?
				window.location = "dashboard.html";
				return false;
			});


			// tags that have data
			jQuery.each(result.fields, function(i) {
				var field = result.fields[i].name;
			    var row = $('<div class="tagdata corner"><div class="tagdataheader">' + field + '</div><div class="val"></div></div>');
				row.find(".val").append(_renderValue(result.fields[i].value));
				$('#Data').append(row);

                // console.dir(wikid);
                // if the field has a process function
                if (wikid[field] && wikid[field].process) {
                    wikid[field].process(result);
                }

			});
			
			// attachments
			if (result.attachments && result.attachments.length) {
				var row = $('<div class="tagdata corner"><div class="tagdataheader">Attachments</div><ul class="val"></div></div>');
				jQuery.each(result.attachments, function(i, attachment) {
					row.find(".val").append('<li><a href="/' + db_name + '/' +  wikid_id + '/' + attachment.name +'">' + attachment.name + '</a></li>');
				});
				$('#Data').append(row);
			}
			
			
			if (result.tagged && result.tagged.length > 0) {
                $("#tagged_title").text("See Also");
				$.each(result.tagged, function(i, tag) {
					var link = $("<li><a href='"+ tag.href +"'>"+ tag.alias + "</a></li>");
					$("#tagged_as").append(link);
				});
			} else {
                $('#tagged_title').hide();
            }
			if (result.tags && result.tags.length > 0) {
				$.each(result.tags, function(i, tag) {
					$('#taglist').append('<a href="' + tag.href +  '">' + tag.alias + '</a><span>&nbsp;</span>');
				});
			} else {
                $('#taglist').hide();
            }
            // freebase
            if (result.freebaseLink ) {
                var row = $('<div class="tagdata corner freebase"><div class="val"></div> <div class="freebase-attribution"> <img alt="Freebase CC-BY" height="23px" style="float: left; border: 0;" width="61px" src="http://www.freebase.com/policies/freebase-cc-by-61x23.png"/> <div style="font-size: x-small; margin-left: 70px; height: 30px; "> Source: <a href="http://www.freebase.com/view' +result.doc['-freebase_id']+ '"><span class="fbs-att-title">Topic</span></a> on <a href="http://www.freebase.com/">Freebase</a>, licensed under <a href="http://creativecommons.org/licenses/by/2.5/">CC-BY</a></div> </div> </div>');
                $.get(result.freebaseLink, null, function(data, status){
                    row.find(".val").html(data.html);
                    $('.fbs-att-title').text($('#fbs-flyout-title').text());
                }, 'jsonp');
                $('#Data').append(row);
            }

            // facebook
            if (result.facebookDetails) {
               var row = $('<div class="tagdata corner"><div class="tagdataheader">Facebook</div> <div class="val"><img class="fbPicture" src="' + result.facebookDetails.picture +'" /></div></div>');
                $.get(result.facebookDetails.graphUrl, null, function(data, status){
                    var toShow = { name: data.name};
                    if (data.gender) toShow.gender = data.gender;
                    row.find(".val").append(_renderValue(toShow));
                    if (data.link) {
                        row.click(function() {
                            window.location = data.link;
                        });
                    }
                }, 'jsonp');
                $('#Data').append(row);
            }
			// expand all rendered json
			$("dt.collapsed").removeClass("collapsed").next().toggle();
            

            $(".spaceName").text(db_name);
            wikid.createTray(db, '#tray');
            wikid.createDragToTray(db, '#titleHolder', '#tray');




			
			$('.skype_tb_injection').live('click', function() {
				//alert('call skype!');
				$("#dialog-skype").dialog('open');
				
			});
			
			// search
			$('#search input').suggest(function(text, callback) {
				$.getJSON("/" + db_name + "/_all_docs?startkey=%22"+ text +"%22&endkey=%22"+ text +"zzz%22&limit=10", function(docs){
					  var matches = [];
					  for (var i = 0; i < docs.rows.length; i++) {
						if (docs.rows[i].id.indexOf(text) == 0) {
						  matches[i] = docs.rows[i].id;
						}
					  }
					  callback(matches);
				});
			}).keypress(function(e) {
				if (e.keyCode == 13) {
					window.location = 'topic.html?' + $(this).val();
				}
			});
			
			// $('#Audiostream-tab a').attr('href', 'audiostream.html?' + wikid_id);			
			$('#Audiostream-tab a').attr('href', '/_greenbridge/tag.html?tag=' + wikid_id);

            // Creating custom :external selector
            $.expr[':'].external = function(obj){
                return !obj.href.match(/^mailto\:/)
                        && (obj.hostname != location.hostname);
            };

            // Add 'external' CSS class to all external links
            $('a:external').addClass('external').attr('target', '_blank');


		});
	 </script>
</head>
<body>
<div class="breadcrumbBar">
    <a href="/">My Spaces</a> >> <a class="spaceName" href="index.html"></a> >> <span class="readableId" href="dashboard.html"></span>
</div>
<div class="logo">
    <img  src="images/corner_logo.png" alt="Eckoit Logo" />
</div>
<div class="colmask holygrail">
    <div class="colmid">
        <div class="colleft">
            <div class="col1wrap">
                <div class="col1">
                    <div class="taboverhead">
                    </div>
                    <div class="tabheader">
                        <div class="maintabs">
                            <ul id="tabnav">
                                <li id="Info-tab"><a href="#" class="maintab active">Topic</a></li>
                                <li id="Audiostream-tab"><a href="#" class="maintab">Audiostream</a></li>
                            </ul>
                        </div>
						<div class="search" id="search">
							<input type="text" name="q" placeholder="Search"/>
						</div>
                    </div>
					<!-- Column 1 start -->
                    <div id="maincontent">
						<div>
						<ul id="menu-h" class="horizontal">
							<li><a id="editButton" href="" title="Edit"><span></span>Edit</a>
							</li>
							<li><a id="addButton" title=""><span></span>Add</a>
								<ul  id="addButtonMenu">

								</ul>
							</li>
							<li><a title=""><span></span>Tools</a>
								<ul  id="toolsButtonMenu">

								</ul>
							</li>
						</ul>
						
						</div>
                        <div id="titleHolder" id="title">
                            <span class="ui-icon ui-icon-grip-dotted-vertical"></span>
                            <span id="title"  class="readableId padcontent" title=""></span>
                        </div>
                        <div id="lastmodified" class="padcontent"></div>
                        <div id="taglist"></div>
                        <div id="Info" class="padcontent content-container">

                         <div>
                            <div id="Data"></div> 
                            <div id="Wiki"></div>

                            <div id="Tagged">
                                <div id="tagged_title" class="padcontent"></div>
                                <ul id="tagged_as">
                                </ul>
                            </div>
                          </div>
                        </div>
                    </div>
					<!-- Column 1 end -->

                </div>
            </div>
            <div class="col2">
				<!-- Column 2 start -->
                <div class="span-3 left">


                    <a href="edit.html">New Topic</a><br/>

                    <div id="tray">

                    </div>



		        </div>
                <!-- Column 2 end -->
            </div>
            <div class="col3">
				<!-- Column 3 start -->
					<div id="sda">
                        <div id="sda_header">
                            Sponsors
                        </div>
                        <div id="create">
                            <a href="http://google.com">Create an Ad.</a>
                        </div>
                        <div class="sponsor">
                            <div class="sp_title"><a href="http://google.com">Healthcare Job Training</a></div>
                            <div class="sp_image"><img src="images/sda/2.jpg" /></div>
                            <div class="sp_body">Quickly become a trained Medical Office Assistant. Quality career training, Fast! Take your first step. Visit us now.</div>
                        </div>
					</div>
				
				<!-- Column 3 end -->
            </div>
        </div>
    </div>
</div>
<div id="dialog-skype" title="Tag Skype Conversation?">
	<p>It appears you are dialing out using the skype plugin. Would you like to tag this conversation?</p>
</div>

</body>
</html>
